package com.ea.eadp.nexus.test.common;

import java.net.URL;

import org.apache.commons.lang.StringUtils;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.LocalFileDetector;
import org.openqa.selenium.remote.RemoteWebDriver;

/**
 * Created with IntelliJ IDEA.
 * User: jianzhou
 * Date: 5/4/14
 * Time: 11:45 AM
 * To change this template use File | Settings | File Templates.
 */
public class BrowserFactory {
     static {
         String os = System.getProperty("os.name");
         if(!os.toLowerCase().startsWith("win")){
             String ChromeDriverPath_linux=SystemProperties.instance().getProperty(SystemProperties.ChromeDriverLinux,"/home/shdev/ChromeWebDriver/chromedriver");
             System.getProperties().setProperty(SystemProperties.ChromeDriver,ChromeDriverPath_linux);
         }
     }
    private static final String REMOTE_WEB_HUB_PROPERTY_NAME = "webdriver.hub.url";

    public enum BrowserType {
        IE, Firefox, Chrome, PhantomJS, RemoteDriver
    }

    public static WebDriver createChromeWebDriver() throws Exception {
        return createChromeWebDriver(new String[] { "start-maximized" });
    }

    public static WebDriver createChromeWebDriver(String[] arguments) throws Exception {
        WebDriver driver = null;
        String remoteConfig = SystemProperties.instance().getProperty(REMOTE_WEB_HUB_PROPERTY_NAME, "");
        if (StringUtils.isNotBlank(remoteConfig)) {
            DesiredCapabilities desiredCapabilities = DesiredCapabilities.chrome();
            driver = new RemoteWebDriver(new URL(remoteConfig), desiredCapabilities);
            ((RemoteWebDriver) driver).setFileDetector(new LocalFileDetector());
        } else {
            ChromeOptions options = new ChromeOptions();
            for (String argument : arguments) {
                options.addArguments(argument);
            }
            driver = new ChromeDriver(options);
        }
        return driver;
    }

    public static WebDriver createChromeWebDriver(ChromeOptions options) throws Exception {
        WebDriver driver = null;
        String remoteConfig = SystemProperties.instance().getProperty(REMOTE_WEB_HUB_PROPERTY_NAME, "");
        if (StringUtils.isNotBlank(remoteConfig)) {
            DesiredCapabilities desiredCapabilities = DesiredCapabilities.chrome();
            driver = new RemoteWebDriver(new URL(remoteConfig), desiredCapabilities);
            ((RemoteWebDriver) driver).setFileDetector(new LocalFileDetector());
        } else {
            driver = new ChromeDriver(options);
        }
        return driver;
    }

    public static WebDriver createChromeWebDriver(DesiredCapabilities capabilities) throws Exception {
        WebDriver driver = null;
        String remoteConfig = SystemProperties.instance().getProperty(REMOTE_WEB_HUB_PROPERTY_NAME, "");
        if (StringUtils.isNotBlank(remoteConfig)) {
            driver = new RemoteWebDriver(new URL(remoteConfig), capabilities);
            ((RemoteWebDriver) driver).setFileDetector(new LocalFileDetector());
        } else {
            driver = new ChromeDriver(capabilities);
        }
        return driver;
    }

    public static WebDriver createWebDriver() throws Exception {
        BrowserType browserType = BrowserType.valueOf(SystemProperties.instance().getProperty("browserType", "Chrome"));

        WebDriver webDriver;

        String remoteConfig = SystemProperties.instance().getProperty(REMOTE_WEB_HUB_PROPERTY_NAME, "");
        if (StringUtils.isNotBlank(remoteConfig)) {
            DesiredCapabilities desiredCapabilities = null;
            switch (browserType) {
                case IE:
                    desiredCapabilities = DesiredCapabilities.internetExplorer();
                    break;
                case Chrome:
                    desiredCapabilities = DesiredCapabilities.chrome();
                    break;
                case Firefox:
                    desiredCapabilities = DesiredCapabilities.firefox();
                    break;
                default:
                    throw new UnsupportedOperationException();
            }
            webDriver = new RemoteWebDriver(new URL(remoteConfig), desiredCapabilities);
            ((RemoteWebDriver)webDriver).setFileDetector(new LocalFileDetector());
            // webDriver = new Augmenter().augment(webDriver);
        } else {
            switch (browserType) {
                case IE:
                    webDriver = new InternetExplorerDriver();
                    break;
                case Chrome:
                    ChromeOptions options = new ChromeOptions();
                    options.addArguments("start-maximized");
                    webDriver = new ChromeDriver(options);
                    break;
               case Firefox:
                    webDriver = new FirefoxDriver();
                    break;
                case PhantomJS:
                    webDriver = new PhantomJSDriver();
                    break;

                default:
                    throw new UnsupportedOperationException();
            }
        }
        if (webDriver.getClass() != ChromeDriver.class) {
            webDriver.manage().window().maximize();
        }
        return webDriver;
    }
}
